package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.LklMerchant;
import com.xmy.cultivate.entity.LklMerchantTeacher;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.TeacherPoint;
import com.xmy.cultivate.service.ILklMerchantService;
import com.xmy.cultivate.service.ILklMerchantStudentService;
import com.xmy.cultivate.service.ILklMerchantTeacherService;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 拉卡拉商户号表 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-03-21
 */
@RestController
@RequestMapping("/api/lklMerchant")
public class LklMerchantController {

    @Autowired
    ILklMerchantStudentService iLklMerchantStudentService;
    @Autowired
    ILklMerchantService iLklMerchantService;
    @Autowired
    ILklMerchantTeacherService iLklMerchantTeacherService;

    @Autowired
    IQuarterService iQuarterService;



    @GetMapping("/getLklMerchantList")
    public Result getLklMerchantList(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                                     @RequestParam(value = "yearPart", required = false) String yearPart,  //年份
                                     @RequestParam(value = "quarterNum", required = false) String quarterNum //学期
                                      ){
//        IPage<LklMerchant> iPage = new Page<>(pageNum, pageSize);
//        QueryWrapper<LklMerchant> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq(StringUtils.isNotBlank(yearPart), "year_part", yearPart);
//        queryWrapper.eq(StringUtils.isNotBlank(quarterNum), "quarter_num", quarterNum);
//        List<LklMerchant> lklMerchantList = iLklMerchantService.list(queryWrapper);
        List<LklMerchant> lklMerchantList = iLklMerchantService.getLklMerchant(yearPart, quarterNum);

        return new Result(Code.OK, lklMerchantList, "查询成功");


    }

    @GetMapping("/getLklMerchantName")
    public Result getLklMerchantName(@RequestParam(value = "schoolId") String schoolId){
        if (StringUtils.isBlank(schoolId)){
            return new Result(Code.EXP_ERROR, null, "schoolId参数不能为空！");

        }
        List<LklMerchant> merchantNameList= iLklMerchantService.getMerchantNameBySchoolId(schoolId);
        return new Result(Code.OK, merchantNameList, "查询成功");
    }



    @PostMapping("/saveLklMerchant")
    public Result saveLklMerchant(@RequestBody LklMerchant lklMerchant){


        QueryWrapper<LklMerchant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("school_id",lklMerchant.getSchoolId());
        queryWrapper.eq("year_part",lklMerchant.getYearPart());
        queryWrapper.eq("quarter_num",lklMerchant.getQuarterNum());
        queryWrapper.eq("status",lklMerchant.getStatus());
        queryWrapper.eq("pid",0);
        long count = iLklMerchantService.count(queryWrapper);
        if (count > 0) {
            return new Result(Code.EXP_ERROR, null, "该校区已存在，无法添加");
        }
        boolean save = iLklMerchantService.save(lklMerchant);
        if (save) {
            return new Result(Code.OK, "", "添加成功");
        }
        return new Result(Code.ERROR,"","添加失败");
    }

    //编辑商户
    @PutMapping("/updateLklMerchant")
    public Result updateLklMerchant(@RequestBody LklMerchant lklMerchant){
        QueryWrapper<LklMerchant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("school_id",lklMerchant.getSchoolId());
        queryWrapper.eq("year_part",lklMerchant.getYearPart());
        queryWrapper.eq("quarter_num",lklMerchant.getQuarterNum());
        queryWrapper.eq("status",lklMerchant.getStatus());
        queryWrapper.eq("pid",0);
        long count = iLklMerchantService.count(queryWrapper);
        if (count > 0) {
            return new Result(Code.EXP_ERROR, null, "该校区已存在，无法添加");
        }
        boolean update = iLklMerchantService.updateById(lklMerchant);
        if (update) {
            return new Result(Code.OK, "", "修改成功");
        }
        return new Result(Code.ERROR,"","修改失败");
    }


    /**
     * 添加子商户号
     * @param lklMerchant
     * @return
     */
    @PostMapping("/saveBelowLklMerchant")
    public Result saveBelowLklMerchant(@RequestBody LklMerchant lklMerchant){
        LklMerchant merchant = iLklMerchantService.getById(lklMerchant.getPid());
        if (Objects.isNull(merchant)){
            return new Result(Code.EXP_ERROR, null, "该商户不存在");
        }
        QueryWrapper<LklMerchant>  queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("school_id",lklMerchant.getSchoolId());
        queryWrapper.eq("year_part",lklMerchant.getYearPart());
        queryWrapper.eq("quarter_num",lklMerchant.getQuarterNum());
        queryWrapper.eq("merchant_set_id",lklMerchant.getMerchantSetId());
        queryWrapper.ne("pid",0);
        queryWrapper.eq("status",lklMerchant.getStatus());
        long count = iLklMerchantService.count(queryWrapper);
        if (count > 0) {
            return new Result(Code.EXP_ERROR, null, "该校区已存在该商户，无法添加");
        }

        lklMerchant.setPid(lklMerchant.getPid());
        lklMerchant.setYearPart(merchant.getYearPart());
        lklMerchant.setQuarterNum(merchant.getQuarterNum());
//        QueryWrapper<LklMerchant> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("school_id",lklMerchant.getSchoolId());
//        long count = iLklMerchantService.count();
//        if (count > 0) {
//            return new Result(Code.EXP_ERROR, null, "该校区已存在，无法添加");
//        }
        boolean save = iLklMerchantService.save(lklMerchant);
        if (save) {
            return new Result(Code.OK, "", "添加成功");
        }
        return new Result(Code.ERROR,"","添加失败");
    }

    //编辑子商户
    @PutMapping("/updateBelowLklMerchant")
    public Result updateBelowLklMerchant(@RequestBody LklMerchant lklMerchant) {
        LklMerchant merchant = iLklMerchantService.getById(lklMerchant.getId());
            if (Objects.isNull(merchant)) {
                return new Result(Code.EXP_ERROR, null, "该商户不存在,无法编辑");
            }
        QueryWrapper<LklMerchant>  queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("school_id",lklMerchant.getSchoolId());
        queryWrapper.eq("year_part",lklMerchant.getYearPart());
        queryWrapper.eq("quarter_num",lklMerchant.getQuarterNum());
        queryWrapper.eq("merchant_set_id",lklMerchant.getMerchantSetId());
        queryWrapper.ne("pid",0);
        queryWrapper.eq("status",lklMerchant.getStatus());
        long count = iLklMerchantService.count(queryWrapper);
        if (count > 0) {
            return new Result(Code.EXP_ERROR, null, "该校区已存在该商户，无法添加");
        }
            boolean update = iLklMerchantService.updateById(lklMerchant);
            if (update) {
                return new Result(Code.OK, "", "修改成功");
            }
            return new Result(Code.ERROR,"","修改失败");
    }

    //删除
    @DeleteMapping("/deleteLklMerchant")
    public Result deleteLklMerchant(@RequestBody LklMerchant lklMerchant){
        LklMerchant lklMerchant1 = iLklMerchantService.getById(lklMerchant.getId());
        if (Objects.isNull(lklMerchant1)){
            return new Result(Code.EXP_ERROR, null, "该商户不存在,无法删除");
        }
        if (lklMerchant1.getPid() == 0){
            QueryWrapper<LklMerchant> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("pid",lklMerchant.getId());
            long count = iLklMerchantService.count(queryWrapper);
            if (count > 0) {
                return new Result(Code.EXP_ERROR, null, "存在子商户，无法删除");
            }
        }else {
            QueryWrapper<LklMerchantTeacher> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("merchant_id",lklMerchant.getId());
            long count = iLklMerchantTeacherService.count(queryWrapper);
            if (count > 0) {
                return new Result(Code.EXP_ERROR, null, "该商户有老师，无法删除");
            }
        }
        boolean remove = iLklMerchantService.removeById(lklMerchant.getId());
        if (remove){
            return new Result(Code.OK, "", "删除成功");
        }
        return new Result(Code.ERROR,"","删除失败");
    }

}
